package xcmg.syn.util;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;

import javax.xml.bind.DatatypeConverter;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.List;
import java.util.TreeMap;
@Slf4j
public class ReqDedupHelper {

    /**
     * @Des
     * @Date 2021/5/24 14:33
     * @Author wangzhaoyu
     * @Param [reqObj ：  请求的参数，这里通常是JSON
     * excludeKeys :求参数里面要去除哪些字段再求摘要]
     * @Return java.lang.String
     */
    public static String dedupParamMD5(final Object reqObj, String... excludeKeys){
        String reqJSON = JSON.toJSONString(reqObj);
        String result = dedupParamMD5(reqJSON, excludeKeys);
        return result;
    }
    /**
     *
     * @param reqJSON 请求的参数，这里通常是JSON
     * @param excludeKeys 请求参数里面要去除哪些字段再求摘要
     * @return 去除参数的MD5摘要
     */
    public static String dedupParamMD5(final String reqJSON, String... excludeKeys) {
        String decreptParam = reqJSON;

        TreeMap paramTreeMap = JSON.parseObject(decreptParam, TreeMap.class);
        if (excludeKeys!=null) {
            List<String> dedupExcludeKeys = Arrays.asList(excludeKeys);
            if (!dedupExcludeKeys.isEmpty()) {
                for (String dedupExcludeKey : dedupExcludeKeys) {
                    paramTreeMap.remove(dedupExcludeKey);
                }
            }
        }

        String paramTreeMapJSON = JSON.toJSONString(paramTreeMap);
        String md5deDupParam = jdkMD5(paramTreeMapJSON);
        log.info("提交参数加密结果{}",md5deDupParam);
        return md5deDupParam;
    }

    private static String jdkMD5(String src) {
        String res = null;
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            byte[] mdBytes = messageDigest.digest(src.getBytes());
            res = DatatypeConverter.printHexBinary(mdBytes);
        } catch (Exception e) {
            log.error("",e);
        }
        return res;
    }

    public static void main(String[] args) {
        String s = "{\"materialsList\":[{\"isChecked\":false,\"barCode\":null,\"marketingActivityPriceVOList\":[{\"activityId\":\"518\",\"name\":\"8折\",\"modeType\":0,\"modeRate\":0.8,\"price\":null,\"discountPrice\":0,\"priceGroup\":null,\"priceType\":\"0\"}],\"materialsId\":\"1093837\",\"materialsNo\":\"310503779.30\",\"materialsDes\":\"XE135G.00 液压挖掘机（装配）\",\"materialsDesEn\":\"Xe135g.00 hydraulic excavator (assembly)\",\"materialsType\":null,\"unit\":\"TAI\",\"price\":0,\"standardPrice\":261823.06,\"customMaterialsNo\":null,\"replaceMaterialsDes\":null,\"replaceMaterialsNo\":null,\"replaceMaterialsNote\":null,\"retrofitMaterialsDes\":null,\"retrofitMaterialsNo\":null,\"sourcePrice\":0,\"enableNum\":0,\"intentionNum\":0,\"deliveryNum\":0,\"locationCode\":null,\"userName\":null,\"priceSchemeId\":null,\"priceSchemeName\":null,\"amount\":null,\"totalStock\":null,\"minimumBuy\":1,\"isMinimumBuy\":0,\"qty\":1,\"quantity\":1,\"amountAfterAdjust\":1,\"applyQty\":1,\"requestDeliveryDate\":\"2021-05-24T06:25:02.003Z\",\"deliveryDate\":\"2021-05-24T06:25:02.003Z\",\"costPrice\":\"261823.06\",\"costSubtotal\":\"261823.06\",\"settlementPrice\":0,\"unitPriceAfterAdjust\":0,\"applyTotalPrice\":0,\"totalPriceAfterAdjust\":0},{\"isChecked\":false,\"barCode\":null,\"marketingActivityPriceVOList\":[{\"activityId\":\"518\",\"name\":\"8折\",\"modeType\":0,\"modeRate\":0.8,\"price\":null,\"discountPrice\":0,\"priceGroup\":null,\"priceType\":\"0\"}],\"materialsId\":\"1093836\",\"materialsNo\":\"380906878\",\"materialsDes\":\"Test Material for Valuation Type评估类型测试\",\"materialsDesEn\":\"Test Material for Valuation Type评估类型测试\",\"materialsType\":null,\"unit\":\"EA\",\"price\":0,\"standardPrice\":0,\"customMaterialsNo\":null,\"replaceMaterialsDes\":null,\"replaceMaterialsNo\":null,\"replaceMaterialsNote\":null,\"retrofitMaterialsDes\":null,\"retrofitMaterialsNo\":null,\"sourcePrice\":0,\"enableNum\":0,\"intentionNum\":0,\"deliveryNum\":0,\"locationCode\":null,\"userName\":null,\"priceSchemeId\":null,\"priceSchemeName\":null,\"amount\":null,\"totalStock\":null,\"minimumBuy\":1,\"isMinimumBuy\":0,\"qty\":1,\"quantity\":1,\"amountAfterAdjust\":1,\"applyQty\":1,\"requestDeliveryDate\":\"2021-05-24T06:25:02.634Z\",\"deliveryDate\":\"2021-05-24T06:25:02.634Z\",\"costPrice\":0,\"costSubtotal\":0,\"settlementPrice\":0,\"unitPriceAfterAdjust\":0,\"applyTotalPrice\":0,\"totalPriceAfterAdjust\":0}],\"isDirectCast\":0,\"forward\":0,\"isImprove\":0,\"createDate\":1621837466950,\"purchaseTypeChildId\":\"\",\"sendTypeChildId\":\"\",\"supplierOrgName\":\"徐州徐工挖掘机械有限公司\",\"supplierOrgId\":\"186089832693366784\",\"purchaseOrgName\":\"谷德平\",\"purchaseOrgId\":\"455551197101686784\",\"directLimit\":\"0\",\"dispatcherName\":\"孙忠伟\",\"dispatcherId\":\"265530898126995456\",\"purchaseType\":\"2\",\"purchaseTypeName\":\"补库\",\"shippingMethodId\":\"PZ\",\"shippingMethod\":\"挖配载\",\"receiverName\":\"谷一\",\"receiverTel\":\"13567652234\",\"receiverAddress\":\"太原市西北区\",\"addressCode\":\"200629-101133-0023\",\"totalValue\":0,\"deliveryDate\":null,\"seller\":true}";
        String s1 = dedupParamMD5(s, null);
        System.out.println(s1);
    }
}
